import vnexpress_cate
import dantri_cate
import zingnews_cate
import kenh14_cate
import vietnamnet_cate
import vtc_cate
import genk_cate
import tiin_cate

import mylib
import datetime
import MySQLdb
import time


conn = mylib.create_conn()

config_point = {
          0:10,
          1:9,
          2:8,
          3:7
          }



def get_hot_cate():
    results = []
    vne_results = vnexpress_cate.get_hot_link()
    dantri_results = dantri_cate.get_hot_link()
    zing_results = zingnews_cate.get_hot_link()
    kenh14_results = kenh14_cate.get_hot_link()
    vietnamnet_results = vietnamnet_cate.get_hot_link()
    vtc_results = vtc_cate.get_hot_link()
    genk_results = genk_cate.get_hot_link()
    tiin_results = tiin_cate.get_hot_link()
    
    hot_10_link = vne_results[1] + dantri_results[1]+vietnamnet_results[1]+vtc_results[1]\
                    +vne_results[0]+dantri_results[0]+vietnamnet_results[0]+vtc_results[0]
    
    print hot_10_link
    hot_9_link = kenh14_results[0]+genk_results[0]+tiin_results[0]+kenh14_results[1]\
                    +genk_results[1]+tiin_results[1]+vne_results[2]+dantri_results[2]
    
    print hot_9_link
    hot_8_link = vietnamnet_results[2]+kenh14_results[2]+vtc_results[2]+genk_results[2]+tiin_results[2]
                    
    print hot_8_link
    hot_7_link = vne_results[3]+dantri_results[3]+zing_results[3]\
                    +vietnamnet_results[3]+kenh14_results[3]+vtc_results[3]+genk_results[3]
    print hot_7_link
    
    results.append(hot_7_link)
    results.append(hot_8_link)
    results.append(hot_9_link)
    results.append(hot_10_link)
    
    return results
    
    
    
    
def process_hot_cate(conn):
    try:
        results = get_hot_cate()
        print results
        
        if results == None:
            print 'Khong lay duoc link'
        else:
            for i in range(0,4):
                point_i = results[i]
                for l in point_i:
                    url_hash = mylib.hash_url(l)
                    hot_query = "CALL get_hot_item('%s')"%(url_hash)
                    hot_item = mylib.get_result_sql(conn, hot_query)
                    for h in hot_item:
                        item_id = h['id']
                        category = h['category']
                        item_time = h['timedate']
                        hostid = h['hostid']
                        item_rank = config_point.get(i)
                        hour_rank = mylib.get_date_hour()
                        insert_query = "CALL insert_hot_cate('%s','%s','%s','%s','%s','%s')"\
                                            %(item_id,hostid,category,item_rank,item_time,hour_rank)
                        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
                        cursor.execute(insert_query)
                        cursor.close()
                        print "insert thanh cong"
        
        print 'Xong tat ca luc '+str(datetime.datetime.now())
        
    except:
        print 'Loi khac'        
    
    

if __name__ == "__main__":
    print 'Waitting...'
    try:
        conn.ping()
    except:
        conn = mylib.create_conn()
    while True:
        conn = mylib.create_conn()
        process_hot_cate(conn)
        print 'Xong tat ca'
        time.sleep(3600)
